//
//  V2HTMLHelper.m
//  V2EX
//
//  Created by 花菜ChrisCai on 2016/6/12.
//  Copyright © 2016年 Chris. All rights reserved.
//

#import "V2HTMLHelper.h"
#import "TFHpple.h"
#import "V2TopicModel.h"
@implementation V2HTMLHelper
+ (NSArray *)dataToObject:(NSData *)data
{

    TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:data];
    
    NSArray *htmlElements;
    
    
    htmlElements  = [xpathParser searchWithXPathQuery:@"//div[@class='cell item']"];
    
    
    NSMutableArray * articleDataArray = [NSMutableArray array];
    for (int i =0; i<htmlElements.count; i++) {
        
        TFHppleElement *element = [htmlElements objectAtIndex:i];
        
        /** avatar */
        
        NSArray *a = [element searchWithXPathQuery:@"//img[@class='avatar']"];
        NSString *avatar = [a[0] objectForKey:@"src"];
        
        /** title */
        NSArray *b = [element searchWithXPathQuery:@"//span[@class='item_title']/a"];
        NSString *artUrl = [[b objectAtIndex:0] objectForKey:@"href"];
        NSString *artTitle = [b[0] content];
        
        /** node */
        NSArray *c = [element searchWithXPathQuery:@"//a[@class='node']"];
        NSString *nodeUrl;
        NSString *nodeName;
        
        nodeName = [c[0] content];
        nodeUrl = [c[0] objectForKey:@"href"];
        
        
        /** user */
        NSArray *d;
        NSString *userMember;
        NSString *userName;
        
        d = [element searchWithXPathQuery:@"//strong/a"];
        userMember = [d[0] objectForKey:@"href"];
        userName = [d[0] content];
        
        
        /** date */
        NSArray *e = [element searchWithXPathQuery:@"//span[@class='small fade']"];
        
        NSString *dateStr;
        
        dateStr = [[e[1] content] componentsSeparatedByString:@"•"][0];
        
        
        //////////////////////
        
        NSString * lhref;
        NSString * lname;
        if (d.count == 2) {
            lhref = [d[0] objectForKey:@"href"];
            lname = [d[0] content];
        }else{
            lhref = @"";
            lname = @"";
        }
        
        /////////////////////
        
        NSArray *f = [element searchWithXPathQuery:@"//a[@class='count_livid']"];
        NSString * rpCount;
        
        
        if (f.count == 1) {
            
            rpCount = [f[0] content];
            
        }else{
            rpCount = @"0";
        }
        
        V2TopicModel *list = [[V2TopicModel alloc] initWithUserAvatar:avatar
                                                                 rUrl:artUrl
                                                             ARTTitle:artTitle
                                                                 NUrl:nodeUrl
                                                                NName:nodeName
                                                                uMber:userMember
                                                                uName:userName
                                                              crtDate:[dateStr stringByTrimmingCharactersInSet:
                                                                       [NSCharacterSet whitespaceAndNewlineCharacterSet]]
                                                                 lrum:lhref
                                                                 lrun:lname
                                                                  RPC:rpCount];
        
        [articleDataArray addObject:list];
    }
    
    
    return articleDataArray;
    
}

@end
